package game.module.sss.procesor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.module.init.logic.RoomConnectManager;
import game.module.user.dao.LanSessionCache;
import lion.common.MsgCodeAnn;
import lion.netty4.message.GamePlayer;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lion.netty4.processor.MsgProcessor;
import lua.LuaMessageSss.C2SSssConnectRoom;
import lua.LuaMessageSss.S2CSssConnectRoom;

@MsgCodeAnn(msgcode = C2SSssConnectRoom.id, accessLimit = 500)
public class SssRoomServerConnectProcessor extends MsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(SssRoomServerConnectProcessor.class);

	@Override
	public void process(GamePlayer gamePlayer, RequestMessageRaw request) throws Exception {
		C2SSssConnectRoom reqMsg = C2SSssConnectRoom.parse(request);
		long roomSessionId = reqMsg.sessionId;
		logger.info("sss room connect,sessionId={}", roomSessionId);
		// 判断session是否有效
		Integer playerId = LanSessionCache.getInstance().getCachePlayerId(roomSessionId);
		if (playerId == null) {
			S2CSssConnectRoom retMsg = new S2CSssConnectRoom(1);
			gamePlayer.writeAndFlush(retMsg.build(gamePlayer.alloc()));
			return;
		}
		// 创建玩家
		RoomConnectManager.cacheNewPlayer(roomSessionId, gamePlayer, playerId);
		// ret
		S2CSssConnectRoom retMsg = new S2CSssConnectRoom(0);
		gamePlayer.writeAndFlush(retMsg.build(gamePlayer.alloc()));
	}

	@Override
	public void process(GamePlayer player, RequestProtoMessage request) throws Exception {
		// null
	}

	@Override
	public void process(GamePlayer player, MyRequestMessage request) throws Exception {
		// null
	}

}
